<template>
	<div class="front-padding orderApprovePage">
		<div class="approveBlock">
			<span>审核结果：</span>
			<el-radio v-model="approveRadio" label="0">审核通过</el-radio>
			<el-radio v-model="approveRadio" label="1">审核未通过</el-radio>
		</div>
		<div class="tab-block-style">
			<span class="tab-block-title">节点选择</span>
			<div class="radio-select-box">
				<div class="left" style="width: 50%;margin-bottom: 15px;"><el-radio v-model="nodeBackRadio" label="1" :disabled="isDisabled">退回到上一步</el-radio></div>
				<div class="right" style="width: 50%;text-align: right;margin-bottom: 15px;"><el-radio v-model="nodeBackRadio" label="2" :disabled="isDisabled">退回到制单人</el-radio></div>
				<div class="left" style="width: 100%;">
					<div class="left"><el-radio v-model="nodeBackRadio" label="3" :disabled="isDisabled">退回到指定节点</el-radio></div>
					<div class="right">
						<el-select v-model="nodeBackSelect" :disabled="nodeBackRadio != 3" class="nodeBackSelect" placeholder="选择回退节点" style="width: 200px!important;">
							<el-option v-for="item in nodeBackArray" :key="item['id']" :label="item['name']" :value="item['id']"></el-option>
						</el-select>
					</div>
				</div>
			</div>
		</div>
		<div class="approveOption">
			<div class="approveOptionTitle">审核意见：</div>
			<el-input type="textarea" v-model="opinion" :rows="3" placeholder="输入审核意见"></el-input>
		</div>
		<div class="btn">
			<el-button type="primary" @click="save">确定</el-button>
		</div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			approveRadio: '0',			//审核结果单选项
			nodeBackRadio: '1',			//节点选择单选项
			nodeBackSelect: '',			//指定回退选项
			opinion: '',
			nodeBackArray: [],			//回退节点数组
			regressionEmpId: null,
			isDisabled: true,			//是否禁用节点选择
		}
	},
	watch: {
		approveRadio(newVal) {
			this.regressionEmpId = null;
			if(newVal === '1') this.nodeBackRadio = '1';
			newVal === '0' ? this.isDisabled = true : this.isDisabled = false;
		},
		nodeBackSelect(newVal) {
			this.regressionEmpId = newVal;
		},
		nodeBackRadio(newVal) {			//监听选中的是否是[回退到指定节点]项
			if(newVal == 1) {
				this.regressionEmpId = null;
			} else if(newVal == 2) {
				this.getUpdateData();		//回退到制单人节点时，regressionEmpId为单据的empId
			} else if(newVal == 3) {
				this.getBackNodeData();
			}
		}
	},
	methods: {
		getUpdateData() {           //获取审核的单据数据
			let rowId = this.$store.state.borrowApproveOrderStore.rowId;
            this.$http.get('loanCategory/findLoanCategoryById?id=' + rowId).then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        this.regressionEmpId = res.data.data.emp;
                    }
                }
            });
        },
		getBackNodeData() {			//获取回退节点数据
			let rowId = this.$store.state.borrowApproveOrderStore.rowId;
			this.$http({
				type: 'get',
				url: 'activiti/showApprovalers',
				params: {billDataId: rowId, billCategoryId: 1}
			}).then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
						this.nodeBackArray = res.data.data;
					}
				}
			});
		},
		save() {			//确定
            let store = this.$store.state.borrowApproveOrderStore;
           	let obj = {
           		flag: Number(this.approveRadio),
           		taskId: store.taskId,
				opinion: this.opinion,
				regressionEmpId: this.regressionEmpId
			}
			if(this.approveRadio === '1') {
				if(this.nodeBackRadio === '3' && !this.regressionEmpId) {
					obj['regressionEmpId'] = this.nodeBackArray[this.nodeBackArray.length-1]['id'];
				}
			} else if(this.approveRadio === '0') {
				obj['regressionEmpId'] = null;
			}
           	this.$http.post('activiti/finishApproval', JSON.stringify(obj), {
           		headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
           	}).then(res => {
           		if(res.data.code == 20001) {
            		this.$message({message: '审核完成', type: 'success', duration: 1500});
            		setTimeout(() => {
                        this.$store.dispatch('borrowApproveOrderStore/getOrderTableDataByBtnStutas');        //刷新表格数据
                        this.close();
                    }, 1000);
            	}
           	});
		},
		close() {
			let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
		}
	}
}
</script>
<style>
	.nodeBackSelect .el-input__inner {height: 34px!important;line-height: 34px!important;}
</style>
<style scoped>
	.approveBlock {margin-bottom: 20px;}
	.tab-block-title {background-color: #f1f1f1;}
	.radio-select-box {overflow: hidden;}
	.nodeBackSelect {margin-top: -5px;}
	.approveOption {margin-bottom: 20px;}
	.approveOptionTitle {margin-bottom: 10px;}
	.approveOption .el-textarea{font-size: 12px;}
	.btn .el-button{width: 100%!important;padding: 10px 10px !important;}
</style>